package com.qingmeng.portal.controller;

import com.qingmeng.common.api.CommonResult;
import com.qingmeng.model.Blog;
import com.qingmeng.portal.model.dto.BlogPublishParam;
import com.qingmeng.portal.model.vo.BlogContentVo;
import com.qingmeng.portal.service.BlogService;
import com.qingmeng.portal.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "BlogController", description = "博客接口")
@RestController
@ResponseBody
@RequestMapping("/blog")
public class BlogController {
    @Autowired
    private BlogService blogService;

    @ApiOperation("发布博客")
    @PostMapping("/publishBlog")
    public CommonResult<Integer> publishBlog(@ApiParam("发布博客时填写的信息")@RequestBody BlogPublishParam param,
                                             @ApiParam("发布者id")@RequestParam("publisherId")Long publisherId) {
        if (blogService.publishBlog(param, publisherId) == 1) {
            return CommonResult.success(1,"发布博客成功");
        }
        return CommonResult.failed("发布博客失败,该用户不存在");
    }

    @ApiOperation("根据id查询博客内容")
    @GetMapping("/getBlogById")
    public CommonResult<BlogContentVo> getBlogById(@ApiParam("博客id")@RequestParam("id") Long id) {
        BlogContentVo blogContentVo = blogService.getBlogById(id);
        if (blogContentVo != null) {
            return CommonResult.success(blogContentVo,"查询成功");
        }
        return CommonResult.failed("该博客不存在或已被删除");
    }

    @ApiOperation("根据标题查询博客内容")
    @GetMapping("/getBlogByTitle")
    public CommonResult<List<BlogContentVo>> getBlogByTitle(@ApiParam("博客标题")@RequestParam("title")String title,
                                                            @ApiParam("页码")@RequestParam(value = "pageNum",defaultValue = "1")int pageNum,
                                                            @ApiParam("每页几条数据")@RequestParam(value = "pageSize",defaultValue = "5")int pageSize) {
        List<BlogContentVo> blogContentVoList = blogService.getBlogByTitle(title, pageNum, pageSize);
        if (blogContentVoList.size() > 0) {
            return CommonResult.success(blogContentVoList,"查询成功");
        }
        return CommonResult.failed("该标题博客不存在或已被删除");
    }

    @ApiOperation("获取所有博客内容")
    @GetMapping("/getAllBlogs")
    public CommonResult<List<BlogContentVo>> getAllBlogs(@ApiParam("页码")@RequestParam(value = "pageNum",defaultValue = "1")int pageNum,
                                                         @ApiParam("每页几条数据")@RequestParam(value = "pageSize", defaultValue = "5")int pageSize) {
        List<BlogContentVo> blogContentVoList = blogService.getAllBlogs(pageNum, pageSize);
        if (blogContentVoList.size() > 0) {
            return CommonResult.success(blogContentVoList,"获取成功");
        }
        return CommonResult.failed("获取失败");
    }

    @ApiOperation("修改博客内容")
    @PostMapping("/alterBlogById")
    public CommonResult<BlogPublishParam> alterBlogById(@ApiParam("博客id")@RequestParam("id")Long id,
                                                        @ApiParam("修改内容")@RequestBody BlogPublishParam param) {
        BlogPublishParam blogPublishParam = blogService.alterBlogById(id, param);
        if (blogPublishParam != null) {
            return CommonResult.success(blogPublishParam,"修改成功");
        }
        return CommonResult.failed("该id的博客不存在或已被删除");
    }

    @ApiOperation("删除博客")
    @GetMapping("/deleteBlogById")
    public CommonResult<Integer> deleteBlogById(@ApiParam("博客id")@RequestParam("id")Long id) {
        int i = blogService.deleteBlogById(id);
        if (i == 1) {
            return CommonResult.success(1,"删除成功");
        }
        return CommonResult.failed("删除失败,该博客不存在或已被删除");
    }
    @ApiOperation("评论并点赞博客")
    @GetMapping("/commentBlog")
    public CommonResult<Integer> commonResult(@ApiParam("博客id")@RequestParam("id") Long id,
                                              @ApiParam("评论")@RequestParam("comment")String comment,
                                              @ApiParam("点赞标签 0表示不点赞，1表示点赞")@RequestParam("tag")int tag,
                                              @ApiParam("评论者id")@RequestParam("userId")Long userId) {
        int i = blogService.commentBlog(id, comment, tag, userId);
        if (i == 1) {
            return CommonResult.success(1,"评论成功");
        }
        return CommonResult.failed("评论失败，该博客不存在或已被删除,或用户不存在");
    }
}
